package com.pudu.mydemo.act;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.pudu.mydemo.view.ShowHidePasswordEditText;
import com.pudu.pdrobot.App;
import com.pudutech.pdrobot.R;


/**
 * Created by guiliu on 2016/7/13.
 */
public class LoginActivity extends BaseHomeActivity implements View.OnClickListener{

    private ImageView login;
    private ImageButton back;
    private RelativeLayout login_layout;
    private ShowHidePasswordEditText password;
    private ImageView logo;
    private LinearLayout linearLayout,login_center;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.login;
    }

    @Override
    protected void onResume() {
        super.onResume();
        AlphaAnimation disapperAnimation = new AlphaAnimation(1.0f, 0.5f);
       // aa.setDuration(800);
     /*   AlphaAnimation disapperAnimation = new AlphaAnimation(1, 0);
        disapperAnimation.setDuration(500);*/
        disapperAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
               // logo.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //logo.setAnimation(disapperAnimation);
        login_layout.setAnimation(disapperAnimation);
    }

    @Override
    public void initView() {
        super.initView();
        back = (ImageButton) findViewById(R.id.back) ;
       linearLayout  = (LinearLayout)findViewById(R.id.linearLayout);
        logo = (ImageView)findViewById(R.id.logo);
        password = (ShowHidePasswordEditText)findViewById(R.id.password);
        password.setTintColor(Color.RED);
        login = (ImageView)findViewById(R.id.login);
        login_layout =(RelativeLayout)findViewById(R.id.login_layout);
        login_center =(LinearLayout) findViewById(R.id.login_center);
    }

    @Override
    public void initEvent() {
        back.setOnClickListener(this);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("android.intent.action.setting");

                startActivity(intent);
                LoginActivity.this.finish();
            }
        });


        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    /*Toast.makeText(getApplicationContext(),"click",Toast.LENGTH_LONG).show();
                    float currentTranslationY = logo.getTranslationY();
                    float logo_height = logo.getHeight();
                    float currentTranslation2Y = login_center.getTranslationY();
                    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(logo, "translationY", currentTranslationY, -800f);
                    ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(login_center,"translationY",currentTranslation2Y,   -450f);
                    AnimatorSet animSet = new AnimatorSet();
                    animSet.play(objectAnimator).with(objectAnimator1);
                    animSet.setDuration(1);
                    animSet.start();
*/



                }
            }
        });

        password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                float currentY = linearLayout.getTranslationY();
                float logo_height = logo.getHeight();
             /*   Toast.makeText(getApplicationContext(),"click",Toast.LENGTH_LONG).show();
                float currentTranslationY = logo.getTranslationY();
                float logo_height = logo.getHeight();
                float currentTranslation3Y = password.getTranslationY();
                float currentTranslation2Y = login_center.getTranslationY();
              ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(logo, "translationY", currentTranslationY, -800f);
                ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(password,"translationY",currentTranslation3Y,   -(password.getBottom()-password.getHeight()));
                ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(login_center, "translationY", currentTranslation2Y,  -(logo_height-password.getHeight()));
                AnimatorSet animSet = new AnimatorSet();
                animSet.play(objectAnimator).with(objectAnimator1).with(objectAnimator3);
                animSet.setDuration(300);
                animSet.start();*/



                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(linearLayout,"translationY", currentY, -(logo_height+getStatusBarHeight()));
                objectAnimator.setDuration(1);
                objectAnimator.start();
             //   linearLayout.setMinimumHeight(login_center.getHeight());

            }
        });
    }


    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
        }
    }
}
